package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.BuyRecord;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.qo.BuyRecordQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.ProductJsonResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IBuyRecordService;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/buyRecord")
public class BuyRecordController {
    @Autowired
    private IBuyRecordService buyRecordService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") BuyRecordQueryObject queryObject, Model model){
        PageInfo<BuyRecord> pageResult = buyRecordService.selectByQuery(queryObject);
        //List<BuyRecord> list = buyRecordService.getAll();
        model.addAttribute("result",pageResult);

        //查所有课程  做回显
        List<Product> products = productService.getAll();
        model.addAttribute("products",products);

        //查出所有潜在客户,做模态框回显选择
        List<Customer> customers = customerService.selectCustomersByPotentialStatus(Customer.STATUS_POTENTIAL);
        model.addAttribute("customers",customers);

        List<Customer> customerList = customerService.getAll();
        model.addAttribute("customerList",customerList);
        return "buyRecord/list";
    }



//    @RequestMapping("/input")
//    public String input(Long id, Model model){
//        if(id != null){
//            BuyRecord buyRecord = buyRecordService.getBuyRecordById(id);
//            model.addAttribute("entity",buyRecord);
//        }
//        return "buyRecord/input";
//    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(BuyRecord buyRecord){
            buyRecordService.saveOrUpdate(buyRecord);
            return "redirect:/buyRecord/list.do";
    }

    @RequestMapping("/productPrice")
    @ResponseBody
    public ProductJsonResult productPrice(Long id){
        //通过产品ID拿到价格返回
        try {
            BigDecimal price = productService.selectPriceById(id);
            return new ProductJsonResult(true,"成功",price);
        }catch(Exception e){
            e.printStackTrace();
            return new ProductJsonResult(false,"获取失败",null);
        }
    }



}
